package com.ykz2015.chapter3.config;

import com.ykz2015.chapter3.conditional.DatabaseConditional;
import com.ykz2015.chapter3.pojo.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration//标明当前这是个配置文件
@ComponentScan//标注扫描策略,默认是当前的配置文件
@ComponentScan("com.ykz2015.chapter3")//全部扫描这个包，就会自动加载所有的@controller，@servcie，@repository @component
public class Appconfig {

    //@Bean(name = "user")//把这个bean装配到ioc容器中，并设置bean的名称为user
    //public User initUser() {
    //    User user = new User();
    //    user.setId(1L);
    //    user.setUserName("user_name_1");
    //    user.setNote("note_1");
    //    return user;
    //
    //}

    //@Bean(name = "user2")//把这个bean装配到ioc容器中，并设置bean的名称为user2
    //public User initUser2() {
    //    User user = new User();
    //    user.setId(2L);
    //    user.setUserName("user_name_2");
    //    user.setNote("note_2");
    //    return user;
    //
    //}

    //@Bean(name = "dataSource", destroyMethod = "close")
    //条件装配bean，利用下面自定义的类的返回值为true才装配这个bean，否则是不装配的
    @Conditional(DatabaseConditional.class)
    public DataSource getDataSoruce(
            @Value("${database.driverName}") String driver,
            @Value("${database.url}") String url,
            @Value("${database.username}") String username,
            @Value("${database.password}") String password
    ) {
        Properties props = new Properties();
        props.setProperty("driver", driver);
        props.setProperty("url", "url");
        props.setProperty("username", "username");
        props.setProperty("password", "password");
        DataSource dataSource = null;
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }
}
